﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Waiter.Common;
using Waiter.Controler;

namespace Waiter.View
{
    public partial class FrmFuniture : DevComponents.DotNetBar.Office2007Form
    {
        public FrmFuniture()
        {
            InitializeComponent();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public void fsLoadCmbStaff()
        {
            StaffCtrl staffCtrl = new StaffCtrl();
            List<STAFF> staffList = staffCtrl.fsGetAllStaffs();
            if (staffList == null || staffList.Count == 0)
            {
                return;
            }

            List<ComboboxItem> itemList = (from s in staffList
                                          select new ComboboxItem()
                                          {
                                              Text = s.NAME,
                                              Value = s.CODE
                                          }).ToList();
            this.cmbStaff.ValueMember = CommonConstants.CMB_VALUE;
            this.cmbStaff.DisplayMember = CommonConstants.CMB_TEXT;
            this.cmbStaff.DataSource = itemList;

            CommonCtrl.fsCmbSetSelectedValue(this.cmbStaff, CommonUtils.fsGetOPID());
        }

        bool floadingFlag = CommonConstants.FLAG_ON;
        private void FrmFuniture_Load(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmFuniture.FrmFuniture_Load", "", "");

            this.floadingFlag = CommonConstants.FLAG_ON;
            try
            {
                fsLoadFunitureList();
                fsLoadFunitureDList();
                fsLoadCmbStaff();
                this.floadingFlag = CommonConstants.FLAG_OFF;

                if (this.dgrList.Rows.Count == 0)
                {
                    this.txtFuniture.Text = "";
                    this.txtFuniture.Focus();
                    this.addFlag = true;
                }
                else
                {
                    dgrList_SelectionChanged(null, null);
                }
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR, "FrmFuniture_Load", "", ex.ToString());
                this.floadingFlag = false;
            }
        }

        private void fsLoadFunitureList()
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmFuniture.fsLoadFunitureList", "", "");

            FunitureCtrl funitureCtrl = new FunitureCtrl();
            List<FUNITURE> funitureList = funitureCtrl.fsGetFunitureList();
            if (funitureList == null || funitureList.Count == 0)
            {
                return;
            }

            int count = funitureList.Count;
            this.dgrList.Rows.Add(count);
            for (int i = 0; i < count; i++)
            {
                this.dgrList.Rows[i].Cells["colFunitureCode"].Value = funitureList[i].CODE;
                this.dgrList.Rows[i].Cells["colFunitureName"].Value = funitureList[i].FUNITURE_NAME;
            }

            this.txtFuniture.Text = this.dgrList.CurrentRow.Cells["colFunitureName"].Value.ToString();
        }

        private void fsLoadFunitureDList()
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmFuniture.fsLoadFunitureDList", "", "");

            this.dgrDetail.Rows.Clear();
            if (this.dgrList.Rows.Count == 0)
            {
                return;
            }

            string funitureCode = this.dgrList.CurrentRow.Cells["colFunitureCode"].Value.ToString();

            FunitureCtrl funitureCtrl = new FunitureCtrl();
            List<FUNITURE_D> funitureDList = funitureCtrl.fsGetFunitureDList(funitureCode);

            if (funitureDList == null || funitureDList.Count == 0)
            {
                return;
            }

            this.dgrDetail.Rows.Add(funitureDList.Count);
            for (int i = 0; i < funitureDList.Count; i++)
            {
                this.dgrDetail.Rows[i].Cells["colDCode"].Value = funitureDList[i].CODE;
                this.dgrDetail.Rows[i].Cells["colDCount"].Value = funitureDList[i].TOTAL_COUNT;
                this.dgrDetail.Rows[i].Cells["colDDate"].Value = funitureDList[i].UPD_DATE;
                this.dgrDetail.Rows[i].Cells["colDUser"].Value = CommonUtils.fsGetOPName(funitureDList[i].OPID);
                this.dgrDetail.Rows[i].Cells["colDNote"].Value = funitureDList[i].NOTE;
                this.dgrDetail.Rows[i].Cells["colDStaffCode"].Value = funitureDList[i].OPID;
            }
        }
        private void fsLoadFunitureInfo()
        {
            if (this.floadingFlag)
            {
                return;
            }

            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmFuniture.fsLoadFunitureInfo", "", "");

            if (this.dgrDetail.Rows.Count == 0)
            {
                this.txtCount.Value = 0;
                this.txtNote.Text = string.Empty;
                return;
            }

            this.txtCount.Text = this.dgrDetail.CurrentRow.Cells["colDCount"].Value.ToString();
            this.txtNote.Text = this.dgrDetail.CurrentRow.Cells["colDNote"].Value.ToString();
            string staffCode = this.dgrDetail.CurrentRow.Cells["colDStaffCode"].Value.ToString();
            CommonCtrl.fsCmbSetSelectedValue(this.cmbStaff, staffCode);
        }

        private void dgrDetail_SelectionChanged(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmFuniture.fsLoadFunitureInfo", "", "");
            fsLoadFunitureInfo();
        }

        bool addFlag = CommonConstants.FLAG_OFF;
        private void btnAddList_Click(object sender, EventArgs e)
        {
            this.txtFuniture.Text = "";
            this.txtFuniture.Focus();
            this.addFlag = CommonConstants.FLAG_ON;
        }

        private void btnSaveList_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmFuniture.btnSaveList_Click", "", "");

            FunitureCtrl funitureCtrl = new FunitureCtrl();

            if (this.txtFuniture.Text == string.Empty)
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("Bạn chưa nhập tên tài sản!", CommonMessages.MSG_ERR_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.txtFuniture.Focus();
                return;
            }

            FUNITURE item = funitureCtrl.fsFindFunitureByName(this.txtFuniture.Text);
            if (item != null)
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("Tài sản này đã có rồi!", CommonMessages.MSG_ERR_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.txtFuniture.Focus();
                return;
            }

            if (this.addFlag == CommonConstants.FLAG_ON)
            {
                item = new FUNITURE();
                item.CODE = CommonUtils.fsCreateNewCode();
                item.FUNITURE_NAME = this.txtFuniture.Text;
                item.DEL_FLG = CommonConstants.FLAG_OFF;
                item.OPID = CommonCtrl.fsCmbGetSelectedValue(this.cmbStaff);
                item.ADD_DATE = DateTime.Now;
                item.UPD_DATE = DateTime.Now;
                if (funitureCtrl.fsInsertFuniture(item) != ResultStatus.SUCCESS)
                {
                    DevComponents.DotNetBar.MessageBoxEx.Show("Đã có lổi xảy ra, vui lòng thực hiện lại!", CommonMessages.MSG_ERR_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    this.txtFuniture.Focus();
                    return;
                }

                this.floadingFlag = CommonConstants.FLAG_ON;
                this.dgrList.Rows.Add(1);
                int idx = this.dgrList.Rows.Count-1;
                this.dgrList.Rows[idx].Cells["colFunitureCode"].Value = item.CODE;
                this.dgrList.Rows[idx].Cells["colFunitureName"].Value = item.FUNITURE_NAME;
                this.floadingFlag = CommonConstants.FLAG_OFF;
                CommonCtrl.fsDgrSetSelectedRow(this.dgrList, idx, 1);
                this.addFlag = CommonConstants.FLAG_OFF;
            }
            else
            {
                string funitureCode = this.dgrList.CurrentRow.Cells["colFunitureCode"].Value.ToString();
                item = funitureCtrl.fsFindFunitureByCode(funitureCode);
                item.FUNITURE_NAME = this.txtFuniture.Text;
                item.OPID = CommonCtrl.fsCmbGetSelectedValue(this.cmbStaff);
                item.UPD_DATE = DateTime.Now;

                if (funitureCtrl.fsSaveChange() != ResultStatus.SUCCESS)
                {
                    DevComponents.DotNetBar.MessageBoxEx.Show("Đã có lổi xảy ra, vui lòng thực hiện lại!", CommonMessages.MSG_ERR_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                this.dgrList.CurrentRow.Cells["colFunitureName"].Value = item.FUNITURE_NAME;
            }
        }

        private void dgrList_SelectionChanged(object sender, EventArgs e)
        {
            if (this.floadingFlag == CommonConstants.FLAG_ON)
            {
                return;
            }

            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmFuniture.dgrList_SelectionChanged", "", "");

            if (this.dgrList.Rows.Count == 0)
            {
                return;
            }

            this.txtFuniture.Text = this.dgrList.CurrentRow.Cells["colFunitureName"].Value.ToString();
            this.addFlag = CommonConstants.FLAG_OFF;
            this.floadingFlag = CommonConstants.FLAG_ON;
            fsLoadFunitureDList();
            this.floadingFlag = CommonConstants.FLAG_OFF;
            fsLoadFunitureInfo();
        }

        private void txtFuniture_Leave(object sender, EventArgs e)
        {
            this.txtFuniture.Text = this.txtFuniture.Text.Trim(); ;
        }

        private void btnDeleteList_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmFuniture.btnDeleteList_Click", "", "");

            if (this.dgrList.Rows.Count == 0)
            {
                return;
            }

            if (DevComponents.DotNetBar.MessageBoxEx.Show("Xóa tài khoản đang chọn?", CommonMessages.MSG_CONFIRM_TITLE, MessageBoxButtons.YesNo, MessageBoxIcon.Question) != System.Windows.Forms.DialogResult.Yes)
            {
                return;
            }

            FunitureCtrl funitureCtrl = new FunitureCtrl();
            string funitureCode = this.dgrList.CurrentRow.Cells["colFunitureCode"].Value.ToString();
            if (funitureCtrl.fsDeleteFuniture(funitureCode) != ResultStatus.SUCCESS)
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("Đã có lổi xảy ra, vui lòng thực hiện lại!", CommonMessages.MSG_ERR_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            int idx = this.dgrList.CurrentRow.Index;
            this.dgrList.Rows.RemoveAt(idx);
        }

        private void btnSaveDetail_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmFuniture.btnSaveDetail_Click", "", "");

            if (this.dgrList.Rows.Count == 0)
            {
                return;
            }

            this.txtNote.Text = this.txtNote.Text.Trim();
            if (this.txtNote.Text == string.Empty)
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("Bạn chưa nhập thông tin ghi chú!", CommonMessages.MSG_ERR_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.txtNote.Focus();
                return;
            }

            if (DevComponents.DotNetBar.MessageBoxEx.Show("Thêm mới thông tin tài sản?", CommonMessages.MSG_CONFIRM_TITLE, MessageBoxButtons.YesNo, MessageBoxIcon.Question) != System.Windows.Forms.DialogResult.Yes)
            {
                return;
            }

            FunitureCtrl funitureCtrl = new FunitureCtrl();
            FUNITURE_D item = new FUNITURE_D();
            item.CODE = CommonUtils.fsCreateNewCode();
            item.FUNITURE_CODE = this.dgrList.CurrentRow.Cells["colFunitureCode"].Value.ToString();
            item.TOTAL_COUNT = this.txtCount.Value;
            item.NOTE = this.txtNote.Text;
            item.OPID = CommonCtrl.fsCmbGetSelectedValue(this.cmbStaff);
            item.ADD_DATE = DateTime.Now;
            item.UPD_DATE = DateTime.Now;

            if (funitureCtrl.fsInsertFunitureD(item) != ResultStatus.SUCCESS)
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("Đã có lổi xảy ra, vui lòng thực hiện lại!", CommonMessages.MSG_ERR_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            this.floadingFlag = CommonConstants.FLAG_ON;
            this.dgrDetail.Rows.Add(1);
            int idx = dgrDetail.Rows.Count - 1;
            this.dgrDetail.Rows[idx].Cells["colDCode"].Value = item.CODE;
            this.dgrDetail.Rows[idx].Cells["colDCount"].Value = item.TOTAL_COUNT;
            this.dgrDetail.Rows[idx].Cells["colDDate"].Value = item.UPD_DATE;
            this.dgrDetail.Rows[idx].Cells["colDUser"].Value = CommonUtils.fsGetOPName(item.OPID);
            this.dgrDetail.Rows[idx].Cells["colDStaffCode"].Value = item.OPID;
            this.dgrDetail.Rows[idx].Cells["colDNote"].Value = item.NOTE;
            this.floadingFlag = CommonConstants.FLAG_OFF;
            CommonCtrl.fsDgrSetSelectedRow(this.dgrDetail, idx, 1);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_INFO, "FrmFuniture.btnSave_Click", "", "");

            if (this.dgrList.Rows.Count == 0)
            {
                return;
            }

            this.txtNote.Text = this.txtNote.Text.Trim();
            if (this.txtNote.Text == string.Empty)
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("Bạn chưa nhập thông tin ghi chú!", CommonMessages.MSG_ERR_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.txtNote.Focus();
                return;
            }

            if (DevComponents.DotNetBar.MessageBoxEx.Show("Lưu lại thay đổi?", CommonMessages.MSG_CONFIRM_TITLE, MessageBoxButtons.YesNo, MessageBoxIcon.Question) != System.Windows.Forms.DialogResult.Yes)
            {
                return;
            }

            string code = this.dgrDetail.CurrentRow.Cells["colDCode"].Value.ToString();
            FunitureCtrl funitureCtrl = new FunitureCtrl();
            FUNITURE_D item = funitureCtrl.fsFindFunitureDByCode(code);
            if (item == null)
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("Đã có lổi xảy ra, vui lòng thực hiện lại!", CommonMessages.MSG_ERR_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            item.TOTAL_COUNT = this.txtCount.Value;
            item.NOTE = this.txtNote.Text;
            item.UPD_DATE = DateTime.Now;
            item.OPID = CommonCtrl.fsCmbGetSelectedValue(this.cmbStaff);

            if (funitureCtrl.fsSaveChange() != ResultStatus.SUCCESS)
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("Đã có lổi xảy ra, vui lòng thực hiện lại!", CommonMessages.MSG_ERR_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            this.dgrDetail.CurrentRow.Cells["colDCount"].Value = item.TOTAL_COUNT;
            this.dgrDetail.CurrentRow.Cells["colDDate"].Value = item.UPD_DATE;
            this.dgrDetail.CurrentRow.Cells["colDUser"].Value = CommonUtils.fsGetOPName(item.OPID);
            this.dgrDetail.CurrentRow.Cells["colDStaffCode"].Value = item.OPID;
            this.dgrDetail.CurrentRow.Cells["colDNote"].Value = item.NOTE;
        }
    }
}
